

/*
program to compute nelson-siegel-svensson forward and zero-coupon rates
*/

********************************************************************************
********************************************************************************
cap program drop calc_nss_yield
program define calc_nss_yield

* parameter arguments, maturity, and forward/zero rate variable names
syntax , beta0(varname) beta1(varname) beta2(varname) beta3(varname) ///
    tau1(varname) tau2(varname) m(real) ///
    [ forwardname(string) zeroname(string) ]

qui {
* forward rates
if "`forwardname'" != "" {
    gen `forwardname' = `beta0' + `beta1'*exp(-`m'/`tau1') + ///
		`beta2'*(`m'/`tau1')*exp(-`m'/`tau1') + ///
		`beta3'*(`m'/`tau2')*exp(-`m'/`tau2')
}
* zero rates
if "`zeroname'" != "" {
    gen `zeroname' = `beta0' + `beta1'*(`tau1'/`m')*(1-exp(-`m'/`tau1')) + ///
		`beta2'*( (`tau1'/`m')*(1-exp(-`m'/`tau1')) - exp(-`m'/`tau1') ) + ///
		`beta3'*( (`tau2'/`m')*(1-exp(-`m'/`tau2')) - exp(-`m'/`tau2') )
}
}

end


********************************************************************************
********************************************************************************
